package yb.ecp.fast.user.infra.datasource;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Properties;

import static com.alibaba.druid.pool.DruidDataSourceFactory.PROP_DRIVERCLASSNAME;
import static com.alibaba.druid.pool.DruidDataSourceFactory.PROP_FILTERS;
import static com.alibaba.druid.pool.DruidDataSourceFactory.PROP_PASSWORD;
import static com.alibaba.druid.pool.DruidDataSourceFactory.PROP_URL;
import static com.alibaba.druid.pool.DruidDataSourceFactory.PROP_USERNAME;

/**
 * @author wanghao
 * @Description
 * @date 2018-03-06 14:15
 */
@Configuration
public class DatasourceConfig {
    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;

    @Bean
    public DataSource getDataSource() throws Exception {
        Properties props = new Properties();
        props.put(PROP_DRIVERCLASSNAME, driverClassName);
        props.put(PROP_URL, url);
        props.put(PROP_USERNAME, username);
        props.put(PROP_PASSWORD, password);
        props.put(PROP_FILTERS, "stat");
        return DruidDataSourceFactory.createDataSource(props);
    }
}
